﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Physis.Client.Models.Sponsor {

    public class SponsorModel : Entity.EntityModel {
                
        #region Private Properties

        private Core.Sponsor.Sponsor sponsor = null;

        #endregion


        #region Public Properties

        public override Int64 EntityId { set { SponsorId = value; } }

        public override Core.Entity.Entity Entity { get { return sponsor; } }


        public Int64 SponsorId {

            set {

                sponsor = Application.Core.Sponsor.Sponsor (value);

                if (sponsor == null) {

                    sponsor = new Core.Sponsor.Sponsor (Application);

                }

            }

        }

        public Core.Sponsor.Sponsor Sponsor { get { return sponsor; } set { sponsor = value; } }


        public System.Web.Mvc.SelectList SponsorTypeSelectList {

            get {

                List<Core.Sponsor.SponsorType> insuranceTypes;

                insuranceTypes = (from currentSponsorType in Application.Core.Sponsor.SponsorTypes (false)

                                  where ((currentSponsorType.Enabled) || (currentSponsorType.Id == Sponsor.SponsorTypeId)) // ENABLED OR IN USE ONLY 

                                  select currentSponsorType).ToList ();

                return new System.Web.Mvc.SelectList (insuranceTypes, "Id", "Name");

            }

        }

        #endregion 


        #region Constructors

        public SponsorModel (Int64 forSponsorId) {

            SponsorId = forSponsorId;

            return;

        }

        public SponsorModel (System.Collections.Specialized.NameValueCollection form) {

            UpdateValues (form);

            return;

        }

        #endregion 


        #region Public Methods

        public override void UpdateValues (System.Collections.Specialized.NameValueCollection form) {

            base.UpdateValues (form);


            // RELOAD BASE OBJECT FROM DATABASE 

            // SponsorId = Data.Readers.FormReader.Int64FromForm (form, "Sponsor.Id");


            // IF NEW INSURER, ASSIGN READ ONLY PROPERTIES 

            if (Sponsor.Id == 0) {

                Sponsor.Name = Data.Readers.FormReader.StringFromForm (form, "Entity.Name");

                Sponsor.SponsorTypeId = Data.Readers.FormReader.Int64FromForm (form, "Sponsor.SponsorTypeId"); 

                // TODO: READ IN INITIAL ENTITY ADDRESSES 

            }


            // READ IN ASSIGNABLE PROPERTIES 

            Sponsor.NationalEmployerId = Data.Readers.FormReader.StringFromForm (form, "Sponsor.NationalEmployerId");

            

            return;

        }

        #endregion 

    }

}